package service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import dao.IInterceptDao;
import entities.TIntercept;
import interfaces.IInterceptService;
import util.ParamUtil;
import utils.StringUtil;

@Service
public class InterceptService implements IInterceptService {

	@Autowired
	private IInterceptDao interceptDao;

	public Boolean exist(String primaryKey) throws Exception {
		if (StringUtil.isEmpty(fetch(primaryKey))) {
			return new Boolean(false);
		}
		return new Boolean(true);
	}

	public Boolean contain(Map<String, Object> paramMap) throws Exception {
		if (StringUtil.isEmpty(single(paramMap))) {
			return new Boolean(false);
		}
		return new Boolean(true);
	}

	public TIntercept fetch(String primaryKey) throws Exception {
		return interceptDao.obtain(primaryKey);
	}

	public TIntercept single(Map<String, Object> paramMap) throws Exception {
		PageHelper.startPage(1, 1, false);
		return interceptDao.unique(paramMap);
	}

	public TIntercept persist(TIntercept entityBean) throws Exception {
		if (StringUtil.isEmpty(entityBean)) {
			return null;
		}
		if (StringUtil.isEmpty(entityBean.getIncNo())) {
			return null;
		}
		if (StringUtil.isEmpty(fetch(entityBean.getIncNo()))) {
			interceptDao.insert(entityBean);
			return entityBean;
		} else {
			interceptDao.update(entityBean);
			return fetch(entityBean.getIncNo());
		}
	}

	public Integer remove(String primaryKey) throws Exception {
		return interceptDao.delete(primaryKey);
	}

	public Integer expunge(Map<String, Object> paramMap) throws Exception {
		return interceptDao.clear(paramMap);
	}

	public Double summary(QuerySet<Object> querySet) throws Exception {
		String polymerizeInfo = querySet.getPolymerizeInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(polymerizeInfo)) {
			return new Double(0);
		}
		paramMap = ParamUtil.polymerizeMap(paramMap, polymerizeInfo);
		Double result = interceptDao.total(paramMap);
		if (StringUtil.isEmpty(result)) {
			return new Double(0);
		}
		return result;
	}

	public Integer record(Map<String, Object> paramMap) throws Exception {
		return interceptDao.count(paramMap);
	}

	public ResultSet<TIntercept> search(QuerySet<Object> querySet) throws Exception {
		ResultSet<TIntercept> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet)) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		SortInfo sortInfo = querySet.getSortInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		if (!StringUtil.isEmpty(sortInfo)) {
			paramMap = ParamUtil.sortMap(paramMap, sortInfo);
		}
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), false);
		List<TIntercept> resultList = interceptDao.query(paramMap);
		pageInfo.setPageRecord(interceptDao.count(querySet.getParamMap()));
		pageInfo.allocation();
		resultSet.setResultList(resultList);
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

}
